
<template>
  <el-dialog :close-on-click-modal="false" :title="$tt('label_tms_waybill_quantity_modification')" :visible.sync="visible" custom-class="dialog_small"
    close-on-click-modal v-loading="loading">
    <el-form label-width="auto" :model="form" :rules="rules" ref="Form">
      <el-form-item :label="$tt('label_tms_adjust_type')" prop="adjustType">
        <!-- <ex_super_input v-model="form.adjustType" type="select" enum="AdjustTypeEnum" class="w_fill" /> -->
        <el-cascader ref="adjust_cascader" v-model="adjust" :options="options" style="width: 100%;"
          @change="cascaderChange"></el-cascader>
      </el-form-item>

      <ex_header_spiler :title="'label_tms_old_info'"></ex_header_spiler>
      <el-form-item :label="$tt('label_total_sku_count')">
        {{ selectItem.totalQuantity }}
      </el-form-item>
      <el-form-item :label="$tt('label_total_box_count')">
        {{ selectItem.totalBox }}
      </el-form-item>
      <el-form-item :label="$tt('label_tms_waybill_total_weight')">
        {{ selectItem.totalWeight }}
      </el-form-item>
      <el-form-item :label="$tt('label_tms_waybill_total_volume')">
        {{ selectItem.totalVolume }}
      </el-form-item>

      <ex_header_spiler :title="'label_tms_adjust_info'"></ex_header_spiler>
      <el-form-item :label="$tt('label_total_sku_count')" prop="totalQuantity">
        <ex_super_input v-model="form.totalQuantity" class="form_input_small" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_waybill_total_cartons')" prop="totalBox">
        <ex_super_input v-model="form.totalBox" class="form_input_small" />
      </el-form-item>
      <el-form-item :label="$tt('label_total_weight')" prop="totalWeight">
        <ex_super_input v-model="form.totalWeight" class="form_input_small" />
      </el-form-item>
      <el-form-item :label="$tt('label_tms_waybill_total_volume')" prop="totalVolume">
        <ex_super_input v-model="form.totalVolume" class="form_input_small" />
      </el-form-item>
      <el-form-item :label="$tt('label_fms_account_cmp_adjust_reason')" prop="reason">
        <ex_super_input v-model="form.reason" class="form_input_small" />
      </el-form-item>

    </el-form>

    <span slot="footer" class="dialog_footer">
      <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submitReady">{{ $tt('label_submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
const default_obj = {
  totalQuantity: null,//		integer 	总件数量
  totalBox: null,//		integer 	总箱数
  totalWeight: null,//double 	总重量
  totalVolume: null,//	double 	总体积
  reason: null,//string 	调整原因
}
export default {
  components: {},
  data() {
    return {
      activeName: "0",
      form: _.cloneDeep(default_obj),
      selectItem: {},
      adjust: [],
      options: [],
      visible: false,

      loading: false,
      item: {},
      rules: {
        reason: { required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' },
        totalQuantity: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.PureNumber(v) || Number.isNaN(Number(v))) {
              return cb(this.$tt('tip_only_positive_pure_decimal'))
            }
            if (Number(v) > Number(this.selectItem.totalQuantity)) {
              return cb(this.$tt('label_tms_not_gt_old_data'))
            }
            cb()
          }, trigger: 'blur'
        },
        totalBox: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.PureNumber(v) || Number.isNaN(Number(v))) {
              return cb(this.$tt('tip_only_positive_pure_decimal'))
            }
            if (Number(v) > Number(this.selectItem.totalBox)) {
              return cb(this.$tt('label_tms_not_gt_old_data'))
            }
            cb()
          }, trigger: 'blur'
        },
        totalWeight: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v,3,9) || Number.isNaN(Number(v))) {
              return cb(this.$tt("tip_check_decimal_digit_int",{digit:3,int:9}))
            }
            if (Number(v) > Number(this.selectItem.totalWeight)) {
              return cb(this.$tt('label_tms_not_gt_old_data'))
            }
            cb()
          }, trigger: 'blur'
        },
        totalVolume: {
          required: true, validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v,2,9) || Number.isNaN(Number(v))) {
              return cb(this.$tt("tip_check_decimal_digit_int",{digit:2,int:9}))
            }
            if (Number(v) > Number(this.selectItem.totalVolume)) {
              return cb(this.$tt('label_tms_not_gt_old_data'))
            }
            cb()
          }, trigger: 'blur'
        },
      },


    }
  },

  methods: {
    cascaderChange(e) {
      let node = this.$refs.adjust_cascader.getCheckedNodes()
      let item = _.head(node)
      if (item && item.data && item.data.item) {
        this.selectItem = item.data.item
      }
    },

    show(item) {
      this.visible = true
      // this.item = Object.assign(_.cloneDeep(item))
      // this.form = {}
      this.item = item
      this.selectItem = item
      this.adjust = ["WHOLE_ORDER"]
      this.form = _.cloneDeep(default_obj)
      this.getData(item.id)
    },
    getData(id) {
      this.loading = true
      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoQueryRpc/selectQtyModifyByWaybillTaskId.json",
        method: 'post',
        data: {
          id
        },
        complete: () => {
          this.loading = false
        },
        success: (res) => {

          this.options = [
            {
              value: 'WHOLE_ORDER',
              label: this.$tt('AdjustTypeEnum__WHOLE_ORDER'),
              item: this.item,
            },
            {
              value: 'DETAILED',
              label: this.$tt('AdjustTypeEnum__DETAILED'),
              children: res.data.rows.map(it => {
                return {
                  value: it.trackId,
                  label: it.trackNumber,
                  item: it,
                  children: it.qtyModifyDTOList.map(qty => {
                    return {
                      value: qty.skuId,
                      label: qty.skuName,
                      item: qty,
                    }
                  })
                }
              })
            }
          ]

        },
      })
    },
    checkBoxRow(it) {
      this.$set(it, 'error', {})
      let check = true
      const _no_empty_key = ['pickUpQuantity', 'pickUpBox', 'isPickUpExceptional',]
      _no_empty_key.forEach(key => {
        if (!it[key]) {
          this.$set(it.error, key, "tip_can_no_empty")
          check = false
        }
      })

      return check
    },
    listValidator(r, v, cb) {
      if (v.length == 0) {
        return cb(this.$tt('tip_least_one_detail'))
      }
      let check = true
      v.forEach(it => {
        if (!this.checkBoxRow(it)) check = false
      })
      if (check) {
        cb()
      } else {
        cb(this.$tt('label_specification_check_fail'))
      }

    },
    hide() {
      this.visible = false
    },
    submitReady() {

      this.$refs['Form'].validate((valid) => {
        if (valid) {
          this.submit()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });
    },
    submit() {
      let node = this.$refs.adjust_cascader.getCheckedNodes()
      let item = _.head(node)

      let data = {
        ...this.form
      }
      if (item.pathNodes.length > 1) {
        data.adjustType = "DETAILED"
        data.trackId = item.data.item.trackId
        data.trackNumber = item.data.item.trackNumber
        data.skuId = item.data.item.skuId
        data.skuName = item.data.item.skuName
        data.id = item.data.item.id
      } else {
        data.adjustType = "WHOLE_ORDER"
      }
      data.waybillTaskId = this.item.id
      data.version = this.item.version

      this.$ajax({
        url: "/tms-api/waybill/TmsTaskInfoOperationRpc/qtyModify.json",
        method: 'post', type: 'json',
        loading: true,
        data,
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.hide()
          this.$emit('submitSuccess')
        },
      })


    },

  }
}
</script>
